/**
 * 教研老师修改年度
 * 
 * /xingzheng/year/update
 */


const Year = require('../../../models/years')
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')

const updata = async function (req, res) {    
    try {
        delete req.body.createdAt
        delete req.body.updatedAt
        delete req.body.type
        delete req.body.id
        
        if (!req.body.year_id) {
            $.sendMessage(res, 1015, 'year_id不能缺少')
            return
        }

        var year = await Year.findOne({
            where: {
                id: req.body.year_id
            }
        })
        
        if (req.body.dead_line) {
            startTime= new Date(Date.parse(year.dataValues.createdAt))
            endTime=new Date(Date.parse(req.body.dead_line))

            if (startTime >= endTime) {
                $.sendMessage(res, 1015, '截止时间不能早于开始时间哦')
                return
            }
        }
        
        var updata = await Year.update(
            req.body,
            {
                where: {
                    id: req.body.year_id
                }
            }
        )

        if (updata[0] == '1') {
            res.json({
                code: 200,
                message: '编辑成功',
            })
            return
        }

        // 异常
        res.json({
            code: 0000,
            message: '代码错误，请联系管理员'
        })
            
        // 日志
        utils.create_a_log(req, '年度管理', '编辑')
        return
    }
    catch(error) {
        $.log('catch error :', error)
        res.json({
            message: "抓到异常",
            error: error
        })
        utils.create_a_log(req, '年度管理', '编辑', '失败')
        return
    } 
}
    
module.exports = updata